package common.utilities;

import java.net.*;
import java.io.*;
import common.logging.WebPortalLogger;

public class URLReader {

	//static String strURL = "http://localhost:80/Index.do?company=Victoria";
   static String strURL = "http://127.0.0.1:9080/clusterapp/test.jsp";

	public URLReader() {
	}

	public boolean isServerAlive(String pStrURL) {
		boolean isAlive = false;
		long t1 = System.currentTimeMillis();
//		WebPortalLogger.info("Start Time: " + t1);

		try {
			URL url = new URL(pStrURL);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				//WebPortalLogger.info(inputLine);
			}
			WebPortalLogger.info("**  Connection successful..  **");
			in.close();
			isAlive = true;
		} catch (Exception e) {
//			WebPortalLogger.printStackTrace();
			WebPortalLogger.info("**  Connection failed..  **");
			isAlive = false;
		}

		long t2 = System.currentTimeMillis();
//		WebPortalLogger.info("End Time: " + t2);
		WebPortalLogger.info("Time taken to check connection: " + (t2-t1) + " ms.");
		return isAlive;
	}

	public static void main(String[] args) throws Exception {
		URLReader test = new URLReader();
		if (test.isServerAlive(strURL)) {
			WebPortalLogger.info("**  IT'S ALIVE  **");
		} else {
			WebPortalLogger.info("**  THE SERVER IS NOT AVAILABLE  **");
		}
	}
}

// End of URLReader.java
